// PongGameView.m
#import "PongGameView.h"

#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#import <UIKit/UIView-Rendering.h>
#import <GraphicsServices/GraphicsServices.h>
#import <UIKit/UIView-Geometry.h>
#import <UIKit/UIView-Gestures.h>
#import <UIKit/UITextView.h>
#import "Common.h"
#import "PongField.h"
CGRect thumbRect;

@implementation PongGameView : UIView

- (id)initWithFrame:(struct CGRect)frame
{
	self = [super initWithFrame:frame];
	if (nil != self)
	{
		LogMethod();
		NSLog(@"PongGameView //// frame = {{%f, %f},{%f, %f}}", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
		//LogMethod();
		//It's not easy being green
		color[0] = 0.0;
		color[1] = 1.0;
		color[2] = 0.0;
		color[3] = 1.0;
		
		float backcomponents[4] = {0, 0, 0, 0};
		CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
		[self setBackgroundColor: CGColorCreate( colorSpace, backcomponents)];
		[self setOpaque:NO];
		thumbRect = CGRectMake(0.0, 0.0, 0.0, 0.0);
		field = nil;
	}
	return self;
}

-(void)dealloc
{
	[field release]; field = nil;
	[super dealloc];
}

- (PongField *)field
{
    //NSLog(@"in -field, returned field = %@", field);
    return field; 
}
- (void)setField:(PongField *)aField
{
    //NSLog(@"in -setField:, old value of field: %@, changed to: %@", field, aField);
    if (field != aField) {
        [aField retain];
        [field release];
        field = aField;
    }
}

- (void)setThumbRect:(CGRect) rect
{
	thumbRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.height + 10, rect.size.height + 10);
	[self setNeedsDisplay];
}

- (void)mouseUp:(struct __GSEvent *)fp8
{
	CGRect rect = CGRectMake(-20.0, -20.0, 0.0, 0.0);
	[self setThumbRect:rect];
    [super mouseUp:fp8];
}

- (void)mouseDown:(struct __GSEvent *)fp8
{
	int chording = 0;
	int clickCount = 0;
	CGRect rect = GSEventGetLocationInWindow(fp8);
	chording = GSEventIsChordingHandEvent(fp8);
	clickCount = GSEventGetClickCount(fp8);
	// NSLog(@"Chording = %i, ClickCount = %i", chording, clickCount);
	// NSLog(@"%@", fp8);
	[self setThumbRect:rect];
    [super mouseDown:fp8];
}

- (void)mouseDragged:(struct __GSEvent *)fp8
{
	CGRect rect = GSEventGetLocationInWindow(fp8);
	[self setThumbRect:rect];
    [super mouseDragged:fp8];
}

- (void)mouseMoved:(struct __GSEvent *)fp8
{
	CGRect rect = GSEventGetLocationInWindow(fp8);
	[self setThumbRect:rect];
    [super mouseMoved:fp8];
}

- (BOOL)canBecomeFirstResponder
{
	 return NO;
}

- (BOOL)respondsToSelector:(SEL)aSelector
{
	 return [super respondsToSelector:aSelector];
}

- (void)forwardInvocation:(NSInvocation *)anInvocation
{
	 [super forwardInvocation:anInvocation];
	 return;
}

- (void)drawRect:(CGRect)frame
{
	CGContextRef ctx = UICurrentContext();
	[field drawInContext:ctx];
    [field drawBackgroundInContext:ctx];
	CGContextFlush( ctx );
}

- (void)setColorRed:(float)r green:(float)g blue:(float)b alpha:(float)a;
{
	color[0] = r;
	color[1] = g;
	color[2] = b;
	color[3] = a;
	[[self field] setColorRed:r green:g blue:b alpha:a];
}

- (float)rotationDegrees
{
    return rotationDegrees;
}
- (void)setRotationDegrees:(float)aRotationDegrees
{
    NSLog(@"in -setRotationDegrees, old value of rotationDegrees: %f, changed to: %f", rotationDegrees, aRotationDegrees);

    rotationDegrees = aRotationDegrees;
	// [self setRotationDegrees:rotationDegrees duration:1.0];
	// [field setFieldRect:[self frame]];
}

- (void)setDelegate:(id)del
{
	_delegate = del;
}

@end
